<?php

error_reporting(E_ALL);
set_time_limit(0);
require_once '../Framework/library/Zend/Dom/Query.php';

class jobspl
{

	private $categoryLinks = array();
	private $offers = array();
	private $import;

	private function show($param)
	{
		echo "<h2>{$param}</h2>";
	}

	private function translateCategory($param)
	{
		return $param;
	}

	private function getContent($link)
	{
		$html = file_get_contents($link);
		$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
		$query = new Zend_Dom_Query('<meta charset="ISO-8859-2"/>' . $html, 'ISO-8859-2');
		try {
			$results = $query->queryXpath('//meta[@name="keywords"]/@content');
			$meta = '';
			foreach ($results as $result) {
				$meta = $result->nodeValue;
			}
			$results = $query->query('#section');
			$output = '';
			foreach ($results as $result) {
				$output .= $result->nodeValue;
			}
		} catch (Zend_Dom_Exception $e) {
			
		}


		return array('content' => trim(mb_substr(preg_replace('/[^(\x20-\x7F)]*/', '', $output), 0, 1000)), 'keywords' => $meta);
	}

	/**
	 * @param Zend_Dom_Query $query
	 */
	private function getFields($query, $category)
	{
		$spr = 0;
		$results = $query->query("#table-blue tr");

		var_dump($results);
		if ($results->count())
		{
			foreach ($results as $result) {
				if ($spr)
				{

					try {

						$helpArray['data_publikacji'] = $this->parseStarDate($result->firstChild->nodeValue);
						$helpArray['data_konca_publikacji'] = $this->parseEndDate();
						$result->removeChild($result->firstChild);
						$helpArray['nazwa_stanowiska'] = $result->firstChild->firstChild->nodeValue;
						$helpArray['url'] = "http://www.jobs.pl" . $result->firstChild->firstChild->getAttribute('href');
						$arr = $this->getContent($helpArray['url']);
						$helpArray['tresc_ogloszenia'] = $arr['content'];
						$helpArray['keywords'] = $arr['keywords'];
						$helpArray['id'] = md5($helpArray['url']);
						$helpArray['firma_publikujaca'] = $result->firstChild->lastChild->nodeValue;
						$result->removeChild($result->firstChild);
						$helpArray['kraj'] = 'Polska';
						$helpArray['region'] = $result->firstChild->lastChild->nodeValue;
						$helpArray['miasto'] = $result->firstChild->firstChild->nodeValue;
						$helpArray['branze'] = $this->translateCategory($category);

						$this->offers[] = $helpArray;
					} catch (Zend_Dom_Exception $e) {
						
					}
				}
				$spr = 1;
			}
		}
	}

	private function parseStarDate($param)
	{
		if (preg_match("|temu$|", $param))
		{
			$param = substr($param, 0, strlen($param) - 8);
			return date("Y-m-d", strtotime(date("Y-m-d") . "-" . $param . "day"));
		}
		else
		{
			return date("Y-m-d", strtotime($param));
		}
	}

	private function parseEndDate()
	{
		return date("Y-m-d", strtotime(date("Y-m-d") . "+3 day"));
	}

	private function openByLink($param, $category)
	{
		$this->show($param);
		$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
		next($this->categoryLinks);
		$query = new Zend_Dom_Query('<?xml encoding="ISO-8859-2">' . $html, 'ISO-8859-2');
		$this->getFields($query, $category);
		$results = $query->query('#table-blue');
		$link = '';
		if ($results->count() >= 2)
		{
			$link = "#subpage-inner-div table[3] tr td[3] p a";
		}
		else
		{
			$link = "#subpage-inner-div table[2] tr td[3] p a";
		}

		$results = $query->query($link);
		foreach ($results as $result) {
			var_dump($result);
			print_r($result);
			$this->openByLink($result->getAttribute('href'), $category);
		}
	}

	private function openByCategory($param, $category)
	{
		$this->categoryLinks[] = "http://www.jobs.pl/wyszukiwanie/-," . str_replace(' ', '%20', $param) . ",-,-,-,0,-,-";
		$html = file_get_contents(current($this->categoryLinks));
		$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
		next($this->categoryLinks);
		$query = new Zend_Dom_Query('<meta charset="ISO-8859-2"/>' . $html, 'ISO-8859-2');
		$this->getFields($query, $category);
		$results = $query->query('#table-blue');
		$link = '';
		if ($results->count() == 2)
		{
			$link = "#subpage-inner-div table[3] tr td[3] p a";
		}
		else
		{
			$link = "#subpage-inner-div table[2] tr td[3] p a";
		}

		$results = $query->query($link);
		foreach ($results as $result) {
			$this->openByLink($result->getAttribute('href'), $category);
		}
	}

	public function __construct()
	{
		$html = file_get_contents('http://www.jobs.pl/jobs.nsf/Strony/szukaj_ofert_pracy');
		$query = new Zend_Dom_Query('<meta charset="ISO-8859-2"/>' . $html, 'ISO-8859-2');
		$results = $query->query("#search table  tr td p[2] span");
		$category = 1;
		foreach ($results as $result) {
			if ($category < 3)
			{
				++$category;
			}
			else
			{
				$this->show($result->nodeValue);
				$this->openByCategory($result->nodeValue, $category++);
			}
		}
	}

	public function generateXML()
	{
		$licznik = 0;
		$this->import = new DOMDocument('1.0', 'ISO-8859-2');
		$this->import->formatOutput = true;
		$this->import->preserveWhiteSpace = false;
		$oferty = $this->import->appendChild($this->import->createElement('offers'));
		foreach ($this->offers as $offer) {
			++$licznik;
			$oferta = $oferty->appendChild($this->import->createElement('offer'));

			$cdata = $this->import->createCDATASection($offer['id']);
			$id = $oferta->appendChild($this->import->createElement('id'));
			$id->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['data_publikacji']);
			$data_publikacji = $oferta->appendChild($this->import->createElement('data_publikacji'));
			$data_publikacji->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['data_konca_publikacji']);
			$data_konca_publikacji = $oferta->appendChild($this->import->createElement('data_konca_publikacji'));
			$data_konca_publikacji->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['nazwa_stanowiska']);
			$nazwa_stanowiska = $oferta->appendChild($this->import->createElement('nazwa_stanowiska'));
			$nazwa_stanowiska->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['tresc_ogloszenia']);
			$tresc_ogloszenia = $oferta->appendChild($this->import->createElement('tresc_ogloszenia'));
			$tresc_ogloszenia->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['firma_publikujaca']);
			$firma_publikujaca = $oferta->appendChild($this->import->createElement('firma_publikujaca'));
			$firma_publikujaca->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['url']);
			$url = $oferta->appendChild($this->import->createElement('url'));
			$url->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['branze']);
			$branze = $oferta->appendChild($this->import->createElement('branze'));
			$branze->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['keywords']);
			$keywords = $oferta->appendChild($this->import->createElement('keywords'));
			$keywords->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['kraj']);
			$kraj = $oferta->appendChild($this->import->createElement('kraj'));
			$kraj->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['region']);
			$region = $oferta->appendChild($this->import->createElement('region'));
			$region->appendChild($cdata);

			$cdata = $this->import->createCDATASection($offer['miasto']);
			$miasto = $oferta->appendChild($this->import->createElement('miasto'));
			$miasto->appendChild($cdata);
		}

		$this->import->save('jobs.xml');
		$this->show("Zaimportowano {$licznik} ogłoszeń");
	}

}

$jobspl = new jobspl();
$jobspl->generateXML();